package com.wans.order;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.wans.order.feign.ProductFeignClient;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import java.util.Arrays;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

// TODO 不知道为什么不会扫描到feign包下的注解 如果这里不添加@EnableFeignClients 就不会扫描到feign包下的注解？
@EnableFeignClients
//@ComponentScan(basePackages = "com.wans.order.feign")  // 扫描feign包下的注解
@SpringBootApplication
public class OrderMainApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderMainApplication.class,args);
    }


    // 配置监听 通过一次性任务
    @Bean
    ApplicationRunner runner(NacosConfigManager nacosConfigManager) {
        return args -> {
            //  获取配置服务
            ConfigService configService = nacosConfigManager.getConfigService();
            // 添加监听
            configService.addListener(
                    "order.properties",
                    "order",
                    new Listener() {
                        // 线程池
                        @Override
                        public Executor getExecutor() {
                            return Executors.newFixedThreadPool(3);
                        }

                        //  变化的配置
                        @Override
                        public void receiveConfigInfo(String config) {
                            System.out.println("监听到配置变化 ->");
                            System.out.println(config);
                        }
                    }
            );


            System.out.println("服务启动完成");
        };
    }
}
